package imse.team0.processors;

import imse.team0.orders.Order;
import org.apache.camel.Exchange;
import org.apache.camel.component.file.GenericFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Eugenia, 4/27/12 9:17 PM
 */
public class InOrderBuilder {

    public void buildOrderToConsole(GenericFile file) {
        Properties properties = new Properties();
        try {
            FileInputStream fis = new FileInputStream((File) file.getFile());
            properties.load(fis);
            System.out.println(properties.getProperty("orderid"));
            System.out.println(properties.getProperty("clientID"));
            System.out.println(properties.getProperty("receiptTime"));
            System.out.println(properties.getProperty("itemId"));
            System.out.println(properties.getProperty("quantity"));

            /*for(String key : properties.stringPropertyNames()) {
           String value = properties.getProperty(key);
           System.out.println(key + " => " + value);
          }  */

            fis.close();
        } catch (IOException e) {
        }
    }

    public void buildOrder(GenericFile file, Exchange exchange) {
        Properties properties = new Properties();
        try {
            FileInputStream fis = new FileInputStream((File) file.getFile());
            properties.load(fis);
            String orderid = properties.getProperty("orderid");
            String clientID = properties.getProperty("clientID");
            String receiptTime = properties.getProperty("receiptTime");
            // String itemId = properties.getProperty("itemId");
            // String quantity = properties.getProperty("quantity");
            String[] itemIds = properties.getProperty("itemId").split(",");
            String[] quantitys = properties.getProperty("quantity").split(",");
            fis.close();

            Order internalOrder = new Order(orderid, clientID, receiptTime);
            for (int i = 0; i < itemIds.length; i++) {
                internalOrder.addItem(new Order.OrderItem(itemIds[i], Integer.parseInt(quantitys[i])));
            }
            exchange.getOut().setBody(internalOrder);
            //exchange.getOut().setHeader("TotalQuantity", exchange.getIn().getHeader("TotalQuantity"));
            exchange.getOut().setHeader("ReceiptTime", receiptTime);
        } catch (IOException e) {
        }
    }


/*    public void readOrder(@Header("TotalQuantity") int totalQty, @Body Order incomingOrder){
    Logger log = LoggerFactory.getLogger(getClass().getName());
    log.info("Received order. ID="+incomingOrder.orderid+", total quantity="+totalQty);
}*/
}
